package com.sitech.portal.system.action;

import java.util.HashMap;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.portal.system.form.PanelConfigForm;
import com.sitech.portal.system.service.ISysPanelConfigService;

public class PanelConfigAction extends JGridBaseAction {
	
	private PanelConfigForm theForm;
	private ISysPanelConfigService sysPanelConfigService;
	private String flag;

	public PanelConfigForm getTheForm() {
		return theForm;
	}

	public void setTheForm(PanelConfigForm theForm) {
		this.theForm = theForm;
	}

	public ISysPanelConfigService getSysPanelConfigService() {
		return sysPanelConfigService;
	}

	public void setSysPanelConfigService(
			ISysPanelConfigService sysPanelConfigService) {
		this.sysPanelConfigService = sysPanelConfigService;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	// 显示Panel列表
	public String loadPanelList() {
		IPage result = sysPanelConfigService.findListByPK(this.getPageForm());
		this.setResult(result);
		return SUCCESS;
	}

	// 编辑Panel列表
	public String editPanel() {
		String[] mIDs = theForm.getPANEL_IDS();
		if (theForm.getISMP_JSP_COMMAND() != null
				&& theForm.getISMP_JSP_COMMAND().equals("add")) {
			theForm.setPANEL_ID(null);
			return SUCCESS;

		}
		if (mIDs != null && mIDs[0] != null) {

			HashMap map = sysPanelConfigService.findObjByPK(mIDs[0]);
			if (map != null) {
				theForm.setPANEL_ID((String) map.get("PANEL_ID"));
				theForm.setPANEL_NAME((String) map.get("PANEL_NAME"));
				theForm.setPANEL_PATH((String) map.get("PANEL_PATH"));
				theForm.setPANEL_HEIGHT((String) map.get("PANEL_HEIGHT"));
				theForm.setPANEL_CODE((String) map.get("PANEL_ID"));
			}
		}
		return SUCCESS;
	}

	// 保存
	public String savePanel() {
		flag = "true";
		try{
			String PANEL_CODE = theForm.getPANEL_CODE(); // 原始的PANEL_ID
			String panID = theForm.getPANEL_ID(); // 改变的PANEL_ID
			HashMap map = sysPanelConfigService.findObjByPK(panID);
			map = (map == null) ? new HashMap() : map;
			map.put("PANEL_NAME", theForm.getPANEL_NAME());
			map.put("PANEL_PATH", theForm.getPANEL_PATH());
			map.put("PANEL_ID", theForm.getPANEL_ID());
			map.put("PANEL_HEIGHT", theForm.getPANEL_HEIGHT());
			map.put("PANEL_CODE", PANEL_CODE);
	
			HashMap cMod = sysPanelConfigService.findObjByPK((String) map
					.get("PANEL_ID"));
	
			if (null == cMod || PANEL_CODE.equals((String) cMod.get("PANEL_ID"))) {
				if (!PANEL_CODE.equals("")) {
					sysPanelConfigService.update(map);
				} else {
					sysPanelConfigService.add(map);
				}
	
			} else {
				flag = "有相同的PanelID!!!";
				return SUCCESS;
			}
		}catch(Exception e){
			flag = e.getMessage();
		}
		return SUCCESS;
	}

	public String delPanel() {
		flag = "true";
		String[] pIDs = theForm.getPANEL_IDS();
		try {
			for (int i = 0; i < pIDs.length; i++) {
				sysPanelConfigService.deleteByPKs(pIDs[i]);
			}

		} catch (Exception e) {
			flag = e.getMessage();
		}

		return SUCCESS;
	}

}
